package com.cci.zkweb.core.controller;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.cci.zkweb.core.entity.SessionItem;
import com.cci.zkweb.core.services.AuthenticationService;
import com.cci.zkweb.core.services.impl.AuthenticationServiceImpl;

public class LoginController 
extends SelectorComposer<Component> {
	private static final long serialVersionUID = 1L;

	//wire components
	@Wire
	Textbox account;
	@Wire
	Textbox password;
	@Wire
	Label message;

	// services
	AuthenticationService authService = new AuthenticationServiceImpl();

	@Listen("onClick=#login; onOK=#loginPane")
	public void doLogin(){
		String nm = account.getValue();
		String pd = password.getValue();

		if(!authService.login(nm,pd)){
			message.setValue("utente o password non corretti!");
			return;
		}
		
		SessionItem si = authService.getUserSessionInfo();
		message.setValue("Benvenuto, " + si.getName());
		message.setSclass("");

		Executions.sendRedirect("/pages/");
	}
}